package itsm.isperp.module.controller.slm;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.message.MessageUtils;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.slm.SlmAlarmSetting;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.db.DbEntityService;
import itsm.isperp.module.service.slm.SlmAlarmSettingService;
import itsm.isperp.module.service.slm.SlmServiceAgreementService;
import itsm.isperp.module.service.slm.SlmServiceTargetService;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("slm/alarmSetting")
public class SlmAlarmSettingController extends BaseController<SlmAlarmSetting> {

	/**
	 * 
	 */
	@Autowired
	protected SlmAlarmSettingService slmAlarmSettingService;

	@Autowired
	protected SlmServiceTargetService slmServiceTargetService;

	@Autowired
	protected AppDictService appDictService;

	@Override
	public BaseService<SlmAlarmSetting> getService() {
		return slmAlarmSettingService;
	}

	@Override
	public String getPageTitle() {
		return "告警提醒";

	}

	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		String agreementId = req.getParameter("agreementId");
		SlmAlarmSetting model = null;
		ModelAndView mv = new ModelAndView();

		Map<String, String> SLM_ALARM_TYPES = new LinkedHashMap<>();

		SLM_ALARM_TYPES.put("提前", "提前");
		SLM_ALARM_TYPES.put("超时", "超时");

		if (StringUtils.isEmpty(id)) {
			model = new SlmAlarmSetting();
			model.setId(HibernateUUIDGenerator.generate());
			model.setDescription("0:0");
			model.setType("提前");
			model.setSendWay("EMAIL,MSG");
		} else {
			model = this.getService().findOne(id);
			// int distance = model.getDistance();

			agreementId = slmServiceTargetService.findOne(
					model.getServiceTargetId()).getAgreementId();

		}

		SlmServiceAgreementService slmServiceAgreementService = ContextHolder
				.getSpringBean("slmServiceAgreementService");

		Map<String, Object> agreement = slmServiceAgreementService
				.findOneToMap(agreementId, "apply_object");

		DbEntityService dbEntityService = ContextHolder
				.getSpringBean("dbEntityService");

		Map<String, Object> dbentity = dbEntityService.findOneToMap(agreement
				.get("apply_object").toString(), "table_name");

		mv.addObject("tableName", dbentity.get("table_name"));

		String[] descriptionArr = model.getDescription().split(":");

		mv.addObject("distinceHours", descriptionArr[0]);
		mv.addObject("distinceMinutes", descriptionArr[1]);

		mv.addObject("pageTitle", this.getPageTitle());

		mv.addObject("model", model);

		mv.addObject(
				"serviceTargets",
				slmServiceTargetService.findAll(
						new JqGridRequest().isPageable(false).addFilterRule(
								"agreement_id", JqGridFilterRuleOp.eq,
								agreementId)).getContent());

		mv.addObject("alarmSettingTypes", SLM_ALARM_TYPES);
		mv.addObject("messageSendWays", MessageUtils.MESSAGE_TYPES);
		mv.setViewName(this.getModulePath() + "/edit");
		return mv;
	}

	@RequestMapping("listByAgreement")
	@ResponseBody
	public PageResponse<Map<String, Object>> listByAgreement(
			JqGridRequest request, HttpServletRequest req) {

		String agreementId = req.getParameter("agreementId");
		if (StringUtils.isEmpty(agreementId)) {
			return null;
		}

		request.isPageable(false).addFilterRule("agreement_id",
				JqGridFilterRuleOp.eq, agreementId);
		return this.getService().findAllDetailByRequest(request).toPage();

	}

	@Override
	@RequestMapping("save")
	@ResponseBody
	public ResultMessage save(SlmAlarmSetting model, HttpServletRequest req) {

		String distinceHours = req.getParameter("distinceHours");
		String distinceMinutes = req.getParameter("distinceMinutes");

		String description = distinceHours + ":" + distinceMinutes;

		model.setDescription(description);

		int elapsed = Integer.parseInt(distinceHours) * 60 * 60
				+ Integer.parseInt(distinceMinutes) * 60;

		// 计算实际时间
		if ("提前".equals(model.getType())) {
			elapsed = -elapsed;
		}

		model.setDistance(elapsed);

		this.getService().saveOrUpdate(model, req);

		return ResultMessage.newInstance(true, null, model.getId());
	}

}
